package com.dubbo.rpcserver;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

/**
 * create by yongli on 2021-10-23 16:49
 */
@ChannelHandler.Sharable
public class EchoHandler extends ChannelInboundHandlerAdapter {

    // 为什么该方法没有发现调用到？
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        System.out.println("this is from read");
        ByteBuf byteBuf = (ByteBuf) msg;
        System.out.println(byteBuf.toString(CharsetUtil.UTF_8));
    }

    // client--server连接起来了，但是没有数据传输
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("被激活");
        super.channelActive(ctx);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        // 读完消息后，还要写一定的消息,还要加入参返回相应的报文

        System.out.println("写回到客户端");
        ctx.writeAndFlush(Unpooled.copiedBuffer("服务端返回了", CharsetUtil.UTF_8));
    }
}

